var OutBalanceStat = function () {
  var template = /* html */ `
    <div class="out-balance-stat" v-loading.fullscreen.lock="loading">
      <search-btn-view append-to-body btn-text="汇总查询" @before-open="resetSearch" @search="searchData">
        <div slot="search-content">
          <search-form :search="statSearch" :dropdown-option="dropdownOption"></search-form>
        </div>
      </search-btn-view>
      <div class="out-balance-stat-info" v-if="warehouseStat">
        <span>未付金额：{{warehouseStat.sumNonReceiptMoney}}</span>
      </div>
      <div class="out-balance-stat-table-div">
        <el-table fit stripe border ref="statTable" :data="warehouseList" style="width: 100%" height="parent">
          <el-table-column label="客户" prop="customerName" min-width="200px" header-align="center"></el-table-column>
          <el-table-column label="总金额" prop="sum" min-width="150px" align="center"></el-table-column>
          <el-table-column label="已收金额" prop="receiptPaymentPrice" min-width="150px" align="center"></el-table-column>
          <el-table-column label="未收金额" prop="nonReceiptPaymentPrice" min-width="150px" align="center"></el-table-column>
        </el-table>
      </div>
    </div>
  `;

  return {
    props: {
      search: {
        type: Object,
        default: function () {
          return {};
        }
      },
      dropdownOption: {
        type: Array,
        default: function () {
          return {};
        }
      }
    },

    data: function () {
      return {
        loading: false,
        warehouseList: [],
        warehouseStat: null,
        statSearch: _.cloneDeep(this.search),
        oldSearch: _.cloneDeep(this.search)
      };
    },

    created: function () {
      this.setWarehouseList(true);
    },

    beforeDestroy: function () {
      this.$emit('stat-closed', this.oldSearch);
    },

    methods: {
      setWarehouseList: function (isSearch) {
        if (!isSearch) {
          this.statSearch = _.cloneDeep(this.oldSearch);
        }

        var postData = {};
        this.setPostData(postData);

        this.loading = true;
        var self = this;
        ajaxRequest('warehouseInSumMoney/getWarehouseOutBalanceSum', postData).then(function (warehouseList) {
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.statSearch);
          }

          var warehouses = [];
          for (var i = 0; i < warehouseList.length; i++) {
            var warehouse = warehouseList[i];
            var sum = warehouse.sum;
            var receiptPaymentPrice = warehouse.receiptPaymentPrice;
            var nonReceiptPaymentPrice = _.round(sum - receiptPaymentPrice, 2);
            if (nonReceiptPaymentPrice <= 0) {
              continue;
            }

            warehouse.nonReceiptPaymentPrice = nonReceiptPaymentPrice;
            warehouses.push(warehouse);
          }

          self.warehouseList = warehouses;
          self.calWarehouseStat();
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      setPostData: function (postData) {
        var search = this.statSearch;
        var fields = ['providerName', 'materialName', 'color', 'model', 'isBalance', 'orderId', 'companyOrderId',
          'customerNo', 'factoryNo', 'createUser', 'startPrice', 'endPrice'
        ];
        SysUtil.setPostData(postData, search, fields);

        var materialDept = search.materialDept;
        if (materialDept !== '') {
          postData.materialDeptId = materialDept.materialDeptId;
        }

        if (search.startDate !== null) {
          postData.startDate = moment(search.startDate).format('YYYY-MM-DD');
        }

        if (search.endDate !== null) {
          postData.endDate = moment(search.endDate).format('YYYY-MM-DD');
        }
      },

      calWarehouseStat: function () {
        var warehouseList = this.warehouseList;
        var sumMoney = 0,
          sumReceiptMoney = 0,
          sumNonReceiptMoney = 0;
        for (var i = 0; i < warehouseList.length; i++) {
          var warehouse = warehouseList[i];
          sumMoney += warehouse.sum;
          sumReceiptMoney += warehouse.receiptPaymentPrice;
          sumNonReceiptMoney += warehouse.nonReceiptPaymentPrice;
        }

        sumMoney = _.round(sumMoney, 2);
        sumReceiptMoney = _.round(sumReceiptMoney, 2);
        sumNonReceiptMoney = _.round(sumNonReceiptMoney, 2);
        this.warehouseStat = {
          sumMoney: sumMoney,
          sumReceiptMoney: sumReceiptMoney,
          sumNonReceiptMoney: sumNonReceiptMoney
        };
      },

      resetSearch: function () {
        this.statSearch = _.cloneDeep(this.oldSearch);
      },

      searchData: function () {
        this.setWarehouseList(true);
      }
    },

    components: {
      SearchBtnView: SearchBtnView,
      SearchForm: SearchForm
    },

    template: template
  };
}();
